﻿Imports System.Data.OleDb
Public Class Details
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then Return
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("HatConnStr").ConnectionString)
        Dim SqlString As String = "Insert into HotelViewedByusers(Hotelfkid,TravellerName) Values (@f1,@f2)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        oleDbConn.Open()
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd.ExecuteNonQuery()

        Dim SqlString2 As String = "Insert into HotelViewedByusersids(Hotelfkid,userfkid) Values (@f1,@f2)"
        Dim cmd2 As OleDbCommand = New OleDbCommand(SqlString2, oleDbConn)
        cmd2.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd2.Parameters.AddWithValue("@f2", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmd2.ExecuteNonQuery()
        oleDbConn.Close()



    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        If tb_Title.Text = "" Or tb_Comment.Text = "" Then
            Exit Sub
        End If
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("HatConnStr").ConnectionString)
        Dim SqlString As String = "Insert into Comments(Author,Title,Comment,HotelID_FK) Values (@f1,@f2,@f3,@f4)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_Title.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Comment.Text)
        cmd.Parameters.AddWithValue("@f4", Request.Params("ID"))
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

        Dim fillcomments As String = "SELECT * FROM  Comments WHERE  HotelID_FK=@f1"
        Dim fillcommentscmd As OleDbCommand = New OleDbCommand(fillcomments, oleDbConn)
        fillcommentscmd.CommandType = CommandType.Text
        fillcommentscmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim dataReader = fillcommentscmd.ExecuteReader()
        GridView1.DataBind()
        tb_Title.Text = ""
        tb_Comment.Text = ""

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("HatConnStr").ConnectionString)
        Dim SqlString As String = "Insert into HotelTagjunction(Hotel_fkind,Tag_fkind) Values (@f1,@f2)"
        oleDbConn.Open()
        For Each cb As ListItem In CheckBoxList1.Items
            If cb.Selected Then
                'join to the table 

                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()

            End If
        Next
        If TextBox1.Text = "" Then Return
        Dim newTags = TextBox1.Text.Split(",")
        For Each newTag In newTags
            'first add new tage to tag table'
            Dim newTagSql As String = "Insert   INTO  Tags(Tag)  VALUES (@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(newTagSql, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", newTag)
            cmd.ExecuteNonQuery()
        Next

        ' Find ID of New Table'
        Dim getlastIdCmd As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID from Tags ORDER BY ID DESC", oleDbConn)
        Dim dr = getlastIdCmd.ExecuteReader()
        dr.Read()
        Dim lastId = dr.GetValue(0)
        ' associate this last id with hotel id '
        Dim newcmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        newcmd.CommandType = CommandType.Text
        newcmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        newcmd.Parameters.AddWithValue("@f2", lastId)
        newcmd.ExecuteNonQuery()







    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
        Dim Conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("HatConnStr").ConnectionString)
        Dim remSql As String = "DELETE from Rating where Username=@f1 and Hotel_fkID=@f2"
        Dim remcmd As OleDbCommand = New OleDbCommand(remSql, Conn)
        Conn.Open()
        remcmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        remcmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
        remcmd.ExecuteNonQuery()
        Dim Sql As String = "Insert into Rating(Username,Hotel_fkID,Rate) Values (@f1,@f2,@f3)"
        Dim addcmd As OleDbCommand = New OleDbCommand(Sql, Conn)

        addcmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        addcmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
        addcmd.Parameters.AddWithValue("@f3", Rating1.CurrentRating)
        addcmd.ExecuteNonQuery()
        Conn.Close()

    End Sub
    Protected Function getstar(ByVal stars As Integer)
        Dim retval As String = ""
        For i = 1 To stars
            retval = retval + "*"
        Next
        Return retval

    End Function



    Protected Sub Btn_like_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_like.Click

    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_like.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("HatConnStr").ConnectionString)
        oleDbConn.Open()
        Dim LikeCmd As OleDbCommand = New OleDbCommand("Insert into HotelLikes(Hotel_fkID, UserName, Liketime) values (@f1, @f2,Now()) ", oleDbConn)
        LikeCmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
        LikeCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        LikeCmd.ExecuteNonQuery()

        img_Like.Visible = True
        Btn_like.Visible = False

        oleDbConn.Close()
        LikeCmd.Dispose()
        oleDbConn.Dispose()




    End Sub
End Class